package weatehrcrowd.servlets;

import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import weathercrowd.managers.PersistenceManager;
import weathercrowd.model.*;
import weathercrowd.utilities.SessionHelper;

/**
 * Servlet implementation class ChartServlet
 */
@WebServlet("/ChartServlet")
public class ChartServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ChartServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGetOrPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGetOrPost(request, response);
	}

	protected void doGetOrPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String sFrom = request.getParameter("datepicker1");
		String sTo = request.getParameter("datepicker2");
		
		//sFrom = "01/06/2007";
		//sTo = "20/02/2010";
		
		
		SimpleDateFormat usformatter = new SimpleDateFormat("MM/dd/yyyy");
		SimpleDateFormat formatter = new SimpleDateFormat("dd/MM/yyyy");
		Date from=null, to=null;
		 
		//System.out.printf("to = %s\n", sTo);
		
		try {
			from = usformatter.parse(sFrom);
			to = usformatter.parse(sTo);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		HttpSession session = request.getSession();
		PersistenceManager pm = SessionHelper.getPersistenceManager(session);
		
		BomLocation locRfBathurst = pm.getLocations("063005").get(0);
		BomLocation locRfCanberra = pm.getLocations("070246").get(0);
		BomLocation locRfWaggaWagga = pm.getLocations("072040").get(0);
		
		BomLocation locTmBathurst =  pm.getLocations("063005").get(0);
		BomLocation locTmCanberra = pm.getLocations("070351").get(0);
		BomLocation locTmWaggaWagga = pm.getLocations("072150").get(0);
		
		String rfBathurst = pm.getRainfallMonthlyXml(from, to, locRfBathurst);
		String rfCanberra = pm.getRainfallMonthlyXml(from, to, locRfCanberra);
		String rfWaggaWagga = pm.getRainfallMonthlyXml(from, to, locRfWaggaWagga);
		
		String tmBathurst = pm.getTemperatureMonthlyXml(from, to, locTmBathurst);
		String tmCanberra = pm.getTemperatureMonthlyXml(from, to, locTmCanberra);
		String tmWaggaWagga = pm.getTemperatureMonthlyXml(from, to, locTmWaggaWagga);
		
		String directory = "D:\\Documents\\quanben\\Projects\\eclipse\\weathercrowdlocal\\WebContent\\lib\\fusion-charts\\data\\";
		FileWriter outFile;
		outFile = new FileWriter(directory+"RainfallBathurst.xml");
		outFile.write(rfBathurst);
		outFile.close();
				
		outFile = new FileWriter(directory+"RainfallCanberra.xml");
		outFile.write(rfCanberra);
		outFile.close();

		outFile = new FileWriter(directory+"RainfallWaggaWagga.xml");
		outFile.write(rfWaggaWagga);
		outFile.close();

		outFile = new FileWriter(directory+"TemperatureBathurst.xml");
		outFile.write(tmBathurst);
		outFile.close();

		outFile = new FileWriter(directory+"TemperatureCanberra.xml");
		outFile.write(tmCanberra);
		outFile.close();

		outFile = new FileWriter(directory+"TemperatureWaggaWagga.xml");
		outFile.write(tmWaggaWagga);
		outFile.close();

		response.sendRedirect("./charts.html");
	}
}
